package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.CalculatePkgUsageUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.iwhalecloud.bss.kite.service.scene.bo.OperateSceneProdInstBO;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.ContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <Description> <br>
 *  端口带宽
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月16日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.handler <br>
 */

public class LocalPortBandwidthAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    IOperateSceneInstService operateSceneInstService;

    @Autowired
    private OperateSceneProdInstBO operateSceneProdInstBO;

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.before(attr, inst, value, data);
        if(data.getTriggerStrategy() != null) {
            // 初始化加载页面时不校验，跳过初次加载时的属性 set 触发
            return;
        }
        if (KiteStringUtils.isEmpty(value)) {
            return;
        }
        String serviceOfferId = data.getServiceOfferId();
        if ("2826".equals(serviceOfferId)) {
            // 产品变更 校验升速、降速变更
            List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "BUSI_CHANGE_SERVICEOFFERID");
            String originValueName = getValueName(attr, attr.getOriginValue());
            String currentValueName = getValueName(attr, value);
            if(StringUtils.isBlank(originValueName) || StringUtils.isBlank(currentValueName) || Objects.isNull(serviceOfferIdList)){
                return;
            }
            //割接数据名称不是数字+单位时不校验
            String originStr = originValueName.replaceAll("[^0-9\\.]", "");
            String currentStr = currentValueName.replaceAll("[^0-9\\.]", "");
            if(KiteStringUtils.isAnyEmpty(originStr,currentStr)){
                return;
            }
            int originUnit = originValueName.endsWith("GBPS")||originValueName.endsWith("G") ? 1024 : 1;
            int currentUnit = currentValueName.endsWith("GBPS")||currentValueName.endsWith("G") ? 1024 : 1;
            double current = Double.parseDouble(currentStr) * currentUnit;
            double origin = Double.parseDouble(originStr) * originUnit;
            if (serviceOfferIdList.contains("1020")){
                // 升速
                if(origin > current) {
                    BssException.throwOut("", "","升速带宽新值不能小于原始带宽值");
                }
            } else if (serviceOfferIdList.contains("1021")){
                // 降速
                if(origin < current) {
                    BssException.throwOut("", "","降速带宽新值不能大于原始带宽值");
                }
            }
        }
    }

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        //端口带宽
        KiteAttr kiteAttr = inst.getAttr("2_0000061");
        // 只有吉林省份端口带宽才有230M,其他省份的去掉
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        if (!KiteStringUtils.equalsAny(provinceCode,LocalKeyConsts.PROVINCE_JILIN,LocalKeyConsts.PROVINCE_GUANGDONG)) {
            //获取端口带宽的枚举值列表
            List<KiteAttrValue> attrValues = kiteAttr.getAttrValues();
            KiteAttrValue kiteAttrValueRemove = null;
            if(attrValues != null && attrValues.size()>0) {
                for (KiteAttrValue kiteAttrValue : attrValues) {
                    //匹配230M的枚举值value:100003794,将其去掉
                    if("100003794".equals(kiteAttrValue.getAttrValue())){
                        kiteAttrValueRemove = kiteAttrValue;
                    }
                }
            }
            if(null != kiteAttrValueRemove){
                attrValues.remove(kiteAttrValueRemove);
            }
        }
       /* // 只有安徽省份端口带宽才有“上下行30 MBPS /100 MBPS”、“上下行50 MBPS /100 MBPS”、“上下行100 MBPS /200 MBPS”,其他省份的去掉
        if (!LocalKeyConsts.PROVINCE_AHHUI.equals(provinceCode)) {
            //获取端口带宽的枚举值列表
            List<KiteAttrValue> attrValues = kiteAttr.getAttrValues();
            KiteAttrValue kiteAttrValueRemove = null;
            KiteAttrValue kiteAttrValueRemove1 = null;
            KiteAttrValue kiteAttrValueRemove2 = null;
            if(attrValues != null && attrValues.size()>0) {
                for (KiteAttrValue kiteAttrValue : attrValues) {
                    if(KiteStringUtils.equals(kiteAttrValue.getAttrValue(),"110009537")){
                        kiteAttrValueRemove = kiteAttrValue;
                    }
                    if(KiteStringUtils.equals(kiteAttrValue.getAttrValue(),"110009538")){
                        kiteAttrValueRemove1 = kiteAttrValue;
                    }
                    if(KiteStringUtils.equals(kiteAttrValue.getAttrValue(),"110009539")){
                        kiteAttrValueRemove2 = kiteAttrValue;
                    }
                }
            }
            if(null != kiteAttrValueRemove){
                attrValues.remove(kiteAttrValueRemove);
            }
            if(null != kiteAttrValueRemove1){
                attrValues.remove(kiteAttrValueRemove1);
            }
            if(null != kiteAttrValueRemove2){
                attrValues.remove(kiteAttrValueRemove2);
            }
        }*/

        super.after(attr,inst,data);
    }

    private static String getValueName (KiteAttr attr, String OriginValue){
        for(KiteAttrValue kiteAttrValue : attr.getAttrValues()) {
            if(kiteAttrValue.getAttrValue().equalsIgnoreCase(OriginValue)) {
                return kiteAttrValue.getAttrValueName();
            }
        }
        return null;
    }

    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        // 若端口带宽 >= 配置阈值，则设置 是否大带宽 默认为“是”
        if (KiteStringUtils.isNotEmpty(value)) {
            String bandWidthDesc = Optional.ofNullable(AttrValueCache.getAttrValue(attr.getAttrId(), value)).map(AttrValue::getAttrValueName).orElse("");
            String provinceNbr = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
            // 阈值配置
            DcPublic dcPublic = DcPublicCache.get("2021121501", provinceNbr);
            String thresholdValueDesc = dcPublic == null ? null : dcPublic.getPcode();
            if(KiteStringUtils.isNotEmpty(bandWidthDesc) && KiteStringUtils.isNotEmpty(thresholdValueDesc)) {
                if (CalculatePkgUsageUtil.compareWidth(bandWidthDesc, thresholdValueDesc)) {

                    // 是否可编辑配置
                    if(KiteStringUtils.equalsAny(dcPublic.getCodea(), KeyConsts.IFTRUE_T, KeyConsts.IFTRUE_F)) {
                        KiteAttr ifBigBandwidthAttr = inst.getAttr("2_0000078");
                        ifBigBandwidthAttr.setIsEdit(dcPublic.getCodea());
                    }
                    operateSceneProdInstBO.updateProdInstAttr(data.getSceneInstId(), inst.getInstId(), "2_0000078",
                        "1_0000106", "是", data.getCustId(), data.getStaffId(), null);
                }
            }
        }

        super.set(attr, inst, value, data);
    }

}
